<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      Promise.myRace = (list) => {
        const len = list.length;
        return new Promise((resolve, reject) => {
          list.forEach((item, index) => {
            Promise.resolve(item)
              .then((res) => {
                resolve(res);
              })
              .catch((e) => {
                reject(e);
              });
          });
        });
      };

      // test
      let p1 = new Promise(function (resolve, reject) {
        setTimeout(function () {
          resolve(1);
        }, 1000);
      });
      let p2 = new Promise(function (resolve, reject) {
        setTimeout(function () {
          reject(2);
        }, 2000);
      });
      let p3 = new Promise(function (resolve, reject) {
        setTimeout(function () {
          resolve(3);
        }, 3000);
      });
      Promise.myRace([p3, p1, p2])
        .then((res) => {
          console.log(res); // [3, 1, 2]
        })
        .catch((err) => {
          console.log(err);
        });
    </script>
  </body>
</html>
